package edu.suda.xsap.controller;
import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.HomePageDto;
import edu.suda.xsap.entity.HomePage;
import edu.suda.xsap.service.HomePageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Year;
import java.time.YearMonth;
import java.util.List;

@RestController
@RequestMapping("/homepage")
@Slf4j
@Tags({@Tag(name = "主页管理", description = "主页管理")})
public class HomePageController {
    @Autowired
    private HomePageService homepageService;

    @Operation(summary = "查询会员", description = "查询所有的会员数量")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/memcount")
    public Result returnMemCount() {
        try {
            Integer memCount = homepageService.getMemCount();
            //
            return Result.success(memCount);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

    @Operation(summary = "查询活跃", description = "查询所有的活跃用户数量")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse()
    })
    @GetMapping("/activecount")
    public Result returnActiveCount() {
        try {
            Integer activeCountDtos = homepageService.getActiveCount();
            //
            return Result.success(activeCountDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "当月金额查询",description = "查询当月的金额充值")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/money")
    public Result returnMoney(){
        try {
            List<HomePageDto> list = homepageService.getMonthlyCharge(Year.of(YearMonth.now().getYear()), YearMonth.now().getMonth());
            return Result.success(list);
        }catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
    @Operation(summary = "查询卡数量",description = "查询卡数量")
    @ApiResponses ({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/card")
    public Result returnCards(){
        try {
            List<HomePageDto> list = homepageService.getCardCount();
            return Result.success(list);
        }catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }

}